/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.property;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;

import java.util.ArrayList;

public class NodeCodePropGenerator extends ClassPropertyGenerator
{
	private String nodeCode;
	public NodeCodePropGenerator(String nodeCode)
	{
		this.nodeCode = nodeCode;
	}

	@Override
	protected TypeInfo getPropertyType() {
		return new TypeInfo(String.class.getTypeName());
	}

	@Override
	protected String getFieldName() {
		return null;
	}

	@Override
	protected String getPropertyName()
	{
		return "NodeCode";
	}

	@Override
	protected boolean isGetterOverride() {
		return true;
	}

	@Override
	protected boolean isSetterOverride() {
		return true;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected boolean hasSetMethod()
	{
		return false;
	}

	@Override
	protected Block buildGetMethodBody() {
		Block block=ast.newBlock();
		if(nodeCode== null||nodeCode.equals(""))
			addReturnNullStatement(block);
		addReturnStatement(block);
		return block;
	}

	private void addReturnNullStatement(Block block) {
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(ast.newNullLiteral());
		block.statements().add(returnStatement);
	}

	private void addReturnStatement(Block block) {

		StringLiteral stringLiteral=ast.newStringLiteral();
		stringLiteral.setLiteralValue(nodeCode);
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(stringLiteral);
		block.statements().add(returnStatement);
	}
}
